package org.agony2d.gpu.supportClasses.buffers {
	import flash.utils.ByteArray;
	import flash.utils.Endian;
	
	import org.agony2d.core.agony_internal;
	import org.agony2d.gpu.core.GroupAA;
	import org.agony2d.gpu.supportClasses.programs.ProgramProxy;
	import org.agony2d.gpu.supportClasses.textures.FullTexture;
	
	use namespace agony_internal;
	
public class BuffersManager {
	
	{
		g_bytes         =  new ByteArray
		g_bytes.endian  =  Endian.LITTLE_ENDIAN
		g_buffersList   =  new <BuffersProp>[]
		g_indexList     =  new <uint>[]
	}
	
	agony_internal static function goFinishAll() : void {
		var index:int
		
		g_totalNodes = g_bytes.position / BYTES_PER_NODE
		if (g_totalNodes > 0) {
			if (g_totalNodes > g_maxNodes) {
				index = g_maxNodes
				g_maxNodes += INCREASE_STEP
				while (g_totalNodes > g_maxNodes) {
					g_maxNodes += INCREASE_STEP
				}
				g_bytes.length = g_maxNodes * BYTES_PER_NODE
				g_indexList.fixed = false
				g_indexList.length = g_maxNodes * 6
				g_indexList.fixed = true
				while (index < g_maxNodes) {
					g_indexList[index * NUM_INDEX_PER_NODE]      =  0 + index * NUM_VERTEX_PER_NODE
					g_indexList[index * NUM_INDEX_PER_NODE + 1]  =  1 + index * NUM_VERTEX_PER_NODE
					g_indexList[index * NUM_INDEX_PER_NODE + 2]  =  2 + index * NUM_VERTEX_PER_NODE
					g_indexList[index * NUM_INDEX_PER_NODE + 3]  =  1 + index * NUM_VERTEX_PER_NODE
					g_indexList[index * NUM_INDEX_PER_NODE + 4]  =  2 + index * NUM_VERTEX_PER_NODE
					g_indexList[index * NUM_INDEX_PER_NODE + 5]  =  3 + index * NUM_VERTEX_PER_NODE
					index++
				}
			}
//			var t:int = getTimer()
			if(g_buffersIndex >= g_buffersLength){
				g_buffersList[g_buffersLength++] = new BuffersProp
//				Logger.reportMessage("BuffersManager", "buffers : " + g_buffersLength)
			}
//			trace("g_buffersIndex : " + g_buffersIndex)
			g_buffers = g_buffersList[g_buffersIndex++]
			g_buffers.uploadData(g_maxNodes)
				
//			trace("time : " + (getTimer() - t))
			g_currProgram.setProperties(g_currFullTex)
			g_currProgram.drawTrianglesAndReset()
		}
	}
	
	agony_internal static function resetAllBuffers() : void {
		//var i:int
		//
		//while (i < m_buffersLength) {
			//g_buffersList[i++].resetData(g_maxNodes)
		//}
		g_buffersList.length = g_buffersLength = 0
	}
	
	
	agony_internal static const BYTES_UV_OFFSET:int      =  8
	agony_internal static const BYTES_COLOR_OFFSET:int   =  16
	agony_internal static const BYTES_TOTAL:int          =  20
	agony_internal static const BYTES_PER_NODE:int       =  80
	agony_internal static const NUM_VERTEX_PER_NODE:int  =  4
	agony_internal static const NUM_INDEX_PER_NODE:int   =  6
	agony_internal static const INCREASE_STEP:int        =  64
	
	agony_internal static var g_indexList:Vector.<uint>
	agony_internal static var g_maxNodes:int
	agony_internal static var g_totalNodes:int
	
	agony_internal static var g_buffers:BuffersProp
	agony_internal static var g_buffersList:Vector.<BuffersProp>
	agony_internal static var g_buffersLength:int
	agony_internal static var g_buffersIndex:int
	agony_internal static var g_bytes:ByteArray
	
	agony_internal static var g_currProgram:ProgramProxy
	agony_internal static var g_currFullTex:FullTexture
	agony_internal static var g_currGroup:GroupAA
}
}